﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Data;

namespace NJL.WebManage.Attractions
{
    public partial class TicketInfo : BasePage
    {
        NJL.BLL.NJL_BOOK bll = new BLL.NJL_BOOK();
        NJL.Model.NJL_BOOK model = new Model.NJL_BOOK();
        NJL.BLL.NJL_CS_BOOK_CLASS bookClass = new BLL.NJL_CS_BOOK_CLASS();  //预订类型
        NJL.BLL.NJL_CS_BOOK_PAY bookPay = new BLL.NJL_CS_BOOK_PAY();        //票务预定付款方式
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                #region 预订类型
                DataSet ds = bookClass.GetList("");
                rblType.DataSource = ds;
                rblType.DataTextField = "TYPE_NAME";
                rblType.DataValueField = "TYPE_CODE";
                rblType.DataBind();
                if (ds.Tables[0].Rows.Count > 0)
                {
                    rblType.Items[0].Selected = true;
                }
                #endregion
                #region 票务预定付款方式
                ds = bookPay.GetList("");
                rblfkfs.DataSource = ds;
                rblfkfs.DataTextField = "TYPE_NAME";
                rblfkfs.DataValueField = "TYPE_CODE";
                rblfkfs.DataBind();
                if (ds.Tables[0].Rows.Count > 0)
                {
                    rblfkfs.Items[1].Selected = true;
                }
                #endregion
                ViewState["state"] = "add";
                bind();

            }
        }
        public void bind()
        {
            rptUserList.DataSource = bll.GetList("TARGET_ID='" + Request.QueryString["id"] + "' and TYPE_CODE='"+Request.Params["typeId"]+"'").Tables[0].DefaultView;
            rptUserList.DataBind();

        }
        protected void rptUserList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                string id = e.CommandArgument.ToString();
                if (bll.Delete(id))
                {
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "删除失败");
                }
            }
            else if (e.CommandName == "Edit")
            {
                #region 预订类型
                DataSet ds = bookClass.GetList("");
                rblType.DataSource = ds;
                rblType.DataTextField = "TYPE_NAME";
                rblType.DataValueField = "TYPE_CODE";
                rblType.DataBind();
                #endregion
                #region 票务预定付款方式
                ds = bookPay.GetList("");
                rblfkfs.DataSource = ds;
                rblfkfs.DataTextField = "TYPE_NAME";
                rblfkfs.DataValueField = "TYPE_CODE";
                rblfkfs.DataBind();
                #endregion
                string id = e.CommandArgument.ToString();
                model = bll.GetModel(id);
                if (model != null)
                {
                    txtName.Text = model.BOOK_NAME;                                         //预订名称
                    txtBookIntro.Text = model.BOOK_INTRO;                                         //预订名称
                    rblType.Items.FindByValue(model.NJL_CS_BOOK_CLASS_TYPE_CODE).Selected = true;              //预订类型
                    rblfkfs.Items.FindByValue(model.NJL_CS_BOOK_PAY_TYPE_CODE).Selected = true;                //付款方式
                    txtAgentPrice.Text = string.Format("{0:0.00}", model.AGENT_PRICE);      //代理价格
                    txtMarketPrice.Text = string.Format("{0:0.00}", model.MARKET_PRICE);    //市场价格
                    txtLocalPrice.Text = string.Format("{0:0.00}", model.LOCAL_PRICE);      //本地价格
                    txtGroupPrice.Text = string.Format("{0:0.00}", model.GROUP_PRICE);      //团购价格
                    txtBackPrice.Text = string.Format("{0:0.00}", model.BACK_PRICE);        //返点金额
                    txtkssl.Text = model.CAN_SALE_COUNT.ToString();                         //可售数量
                    txtyssl.Text = model.SALED_COUNT.ToString();                            //已售数量
                    txtyxksrq.Text = model.EFFECTIVE_START_DATE.ToString();                 //有效开始日期
                    txtyxjsrq.Text = model.EFFECTIVE_END_DATE.ToString();                   //有效结束日期
                    txtgpxz.Text = model.TICKET_NOTICE;                                     //购票须知
                    txtydjzsj.Text = string.Format("{0:HH:mm:ss}",model.THAT_DAY_DEADLINE);                               //截止时间
                    txtMarketPrice.Text = string.Format("{0:0.00}", model.MARKET_PRICE);
                    txtLocalPrice.Text = string.Format("{0:0.00}", model.LOCAL_PRICE);
                    txtBackPrice.Text = string.Format("{0:0.00}", model.BACK_PRICE);
                    txtSort.Text = model.SORT.ToString();
                    txtGroupMinCount.Text = model.GROUP_MIN_COUNT.Value.ToString();//组团最少人数

                    //取票地点
                    txtPickBookAddress.Text = model.PICK_TICKET_ADDRESS;

                    ViewState["state"] = "update";
                    ViewState["ID"] = id;
                    Reset1.Value = "返回";
                    Reset1.Attributes.Add("onclick", "location.href='" + Request.RawUrl + "'");
                }
            }
        }
        protected void rptUserList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Literal lit = e.Item.FindControl("litNum") as Literal;
            lit.Text = (e.Item.ItemIndex + 1).ToString();
        }
        protected void btnDel_Click1(object sender, EventArgs e)
        {
            string[] cbxid = HSelectId.Value.Split(',');
            foreach (string id in cbxid)
            {
                if (bll.Delete(id))
                {
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "删除失败");
                }
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (ViewState["state"].ToString() == "add")
            {
                model.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
                model.NJL_SYS_BASE_ID = WEBSITE_CODE;
                model.BOOK_NAME = txtName.Text.Trim();
                model.BOOK_INTRO = txtBookIntro.Text.Trim();
                model.TYPE_CODE = Request.QueryString["typeId"];  //景区
                model.NJL_CS_BOOK_CLASS_TYPE_CODE = rblType.SelectedValue;              //预订类型
                model.NJL_CS_BOOK_PAY_TYPE_CODE = rblfkfs.SelectedValue;                //付款方式
                model.AGENT_PRICE = Convert.ToDecimal(txtAgentPrice.Text.Trim());       //代理价格
                model.MARKET_PRICE = Convert.ToDecimal(txtMarketPrice.Text.Trim());     //市场价格
                model.LOCAL_PRICE = Convert.ToDecimal(txtLocalPrice.Text.Trim());       //本地价格
                model.GROUP_PRICE = Convert.ToDecimal(txtGroupPrice.Text.Trim());       //团购价格
                model.BACK_PRICE = Convert.ToDecimal(txtBackPrice.Text);                //返点金额
                model.SORT = int.Parse(txtSort.Text.Trim());
                model.GROUP_MIN_COUNT = int.Parse(txtGroupMinCount.Text.Trim());//组团最少人数
                if (txtkssl.Text.Trim() != "")
                {
                    model.CAN_SALE_COUNT = Convert.ToInt32(txtkssl.Text.Trim());             //可售数量
                }
                if (txtyssl.Text.Trim() != "")
                {
                    model.SALED_COUNT = Convert.ToInt32(txtyssl.Text.Trim());               //已售数量
                }
                if (txtyxksrq.Text.Trim() != "")
                {
                    model.EFFECTIVE_START_DATE = Convert.ToDateTime(txtyxksrq.Text.Trim()); //有效开始日期
                }
                if (txtyxjsrq.Text.Trim() != "")
                {
                    model.EFFECTIVE_END_DATE = Convert.ToDateTime(txtyxjsrq.Text.Trim());   //有效结束日期
                }
                if (txtydjzsj.Text.Trim() != "")
                {
                    model.THAT_DAY_DEADLINE = Convert.ToDateTime(txtydjzsj.Text.Trim());    //预订截止时间
                }
                model.TICKET_NOTICE = txtgpxz.Text.Trim();                              //购票须知
                model.CREATE_DATE_TIME = DateTime.Now;
                model.CREATE_USER_ID = USER_ID;
                model.CREATE_USER_NAME = USER_NAME;
                model.OPTION_ROLE = "1";    //操作角色
                model.TARGET_ID = Request.QueryString["id"];                        //目标ID

                model.PICK_TICKET_ADDRESS = txtPickBookAddress.Text.Trim();     //取票地址，用于短信

                if (bll.Add(model))
                {
                    Royalsoft.Common.MessageBox.ShowAndRedirect(this, "添加成功", Request.RawUrl);
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "添加失败");
                }
            }
            else
            {
                model = bll.GetModel(ViewState["ID"].ToString());
                model.BOOK_NAME = txtName.Text.Trim();                                  //预订名称
                model.BOOK_INTRO = txtBookIntro.Text.Trim();                            //预定说明
                model.NJL_CS_BOOK_CLASS_TYPE_CODE = rblType.SelectedValue;              //预订类型
                model.NJL_CS_BOOK_PAY_TYPE_CODE = rblfkfs.SelectedValue;                //付款方式
                model.AGENT_PRICE = Convert.ToDecimal(txtAgentPrice.Text.Trim());       //代理价格
                model.MARKET_PRICE = Convert.ToDecimal(txtMarketPrice.Text.Trim());     //市场价格
                model.LOCAL_PRICE = Convert.ToDecimal(txtLocalPrice.Text.Trim());       //本地价格
                model.GROUP_PRICE = Convert.ToDecimal(txtGroupPrice.Text.Trim());       //团购价格
                model.BACK_PRICE = Convert.ToDecimal(txtBackPrice.Text);                //返点金额
                model.SORT = int.Parse(txtSort.Text.Trim());
                model.GROUP_MIN_COUNT = int.Parse(txtGroupMinCount.Text.Trim());//组团最少人数
                if (txtkssl.Text.Trim() != "")
                {
                    model.CAN_SALE_COUNT = Convert.ToInt32(txtkssl.Text.Trim());            //可售数量
                }
                if (txtyssl.Text.Trim() != "")
                {
                    model.SALED_COUNT = Convert.ToInt32(txtyssl.Text.Trim());               //已售数量
                }
                if (txtyxksrq.Text.Trim() != "")
                {
                    model.EFFECTIVE_START_DATE = Convert.ToDateTime(txtyxksrq.Text.Trim()); //有效开始日期
                }
                if (txtyxjsrq.Text.Trim() != "")
                {
                    model.EFFECTIVE_END_DATE = Convert.ToDateTime(txtyxjsrq.Text.Trim());   //有效结束日期
                }
                if (txtydjzsj.Text.Trim() != "")
                {
                    model.THAT_DAY_DEADLINE = Convert.ToDateTime(txtydjzsj.Text.Trim());    //预订截止时间
                }
                model.TICKET_NOTICE = txtgpxz.Text.Trim();                                   //购票须知
                model.UPDATE_DATE_TIME = DateTime.Now;
                model.UPDATE_USER_ID = USER_ID;
                model.UPDATE_USER_NAME = USER_NAME;

                model.PICK_TICKET_ADDRESS = txtPickBookAddress.Text.Trim();     //取票地址，用于短信

                if (bll.Update(model))
                {
                    Royalsoft.Common.MessageBox.ShowAndRedirect(this, "更新成功", Request.RawUrl);
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "更新失败");
                }
            }
        }
    }
}